package com.easylinkin.sm.adapter;

import com.easylinkin.sm.entity.Customer;
import com.easylinkin.sm.entity.Department;
import com.easylinkin.sm.entity.User;
import com.easylinkin.sm.entity.UserLoginInfo;
import com.easylinkin.sm.service.CustomerService;
import com.easylinkin.sm.service.DepartmentService;
import com.easylinkin.sm.service.UserLoginInfoService;

import static com.easylinkin.sm.constant.LoginConstant.Error.CUSTOMER_LOCKED;
import static com.easylinkin.sm.constant.LoginConstant.Error.CUSTOMER_LOGIN_FAILED_LOCKED;
import static com.easylinkin.sm.constant.LoginConstant.Error.CUSTOMER_NO_FOUND;
import static com.easylinkin.sm.constant.LoginConstant.Error.DEPARTMENT_NO_FOUND;
import static com.easylinkin.sm.constant.LoginConstant.Error.NO_CUSTOMER;
import static com.easylinkin.sm.constant.LoginConstant.Error.NO_DEPARTMENT;

import java.util.Date;
import java.util.Objects;
import javax.annotation.Resource;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Component;
import site.morn.bean.annotation.Target;
import site.morn.exception.ApplicationMessages;
import site.morn.framework.context.LoginValidator;
import site.morn.util.DataUtils;

/**
 * 登录校验器
 *
 * @author TianGanLin
 * @since 1.0.0, 2019/9/12
 */
@Component
@Target(User.class)
public class SmLoginValidator implements LoginValidator<User> {

  @Resource
  private CustomerService customerService;

  @Resource
  private DepartmentService departmentService;

  @Resource
  private UserLoginInfoService userLoginInfoService;

  @Override
  public boolean validate(User data) {
    // 校验所属机构
//    Long departmentId = data.getDepartmentId();
//    if (Objects.isNull(departmentId)) {
//      throw ApplicationMessages.translateException(NO_DEPARTMENT);
//    }
//    Department department = departmentService.get(departmentId);
//    if (Objects.isNull(department) || !DataUtils.isDisplay(department)) {
//      throw ApplicationMessages.translateException(DEPARTMENT_NO_FOUND);
//    }
    // 校验所属客户
//    Long customerId = department.getCustomerId();
//    if (Objects.isNull(customerId)) {
//      throw ApplicationMessages.translateException(NO_CUSTOMER);
//    }
//    Customer customer = customerService.get(customerId);
//    if (Objects.isNull(customer) || !DataUtils.isDisplay(customer)) {
//      throw ApplicationMessages.translateException(CUSTOMER_NO_FOUND);
//    }
//    Boolean locked = customer.getLocked();
//    if (BooleanUtils.isTrue(locked)) {
//      throw ApplicationMessages.translateException(CUSTOMER_LOCKED);
//    }
    // 根据登录信息表规定时间内错误次数判断是否被锁定
    UserLoginInfo userLoginInfo = userLoginInfoService.get(data.getId());
    if (Objects.nonNull(userLoginInfo) && Objects.nonNull(userLoginInfo.getLockTime())) {
      Long minute = ((new Date().getTime() - userLoginInfo.getLockTime().getTime()) / (1000 * 60));
      if (userLoginInfo.getErrorCount() >= 5 && minute < 30) {
        throw ApplicationMessages.translateException(CUSTOMER_LOGIN_FAILED_LOCKED);
      }
    }
    return true;
  }
}
